package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.PacPatentinfoDao;
import com.baitengsoft.patentapimonitor.api.dao.RptpatentDataAreaDao;
import com.baitengsoft.patentapimonitor.api.entity.PacPatentinfoEntity;
import com.baitengsoft.patentapimonitor.api.entity.RptPatentDataAreaEntity;
import com.baitengsoft.patentapimonitor.api.model.WebPatentApplyInfo;
import com.baitengsoft.patentapimonitor.api.model.WebPatentApplyModel;
import com.baitengsoft.patentapimonitor.api.model.param.WebPatentApplySearchParam;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.api.service.WebPatentApplyService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 专利申请查询服务类
 * @author Chen 2018/1/8
 */
@Service
public class WebPatentApplyServiceImpl implements WebPatentApplyService {
    @Autowired
    private RptpatentDataAreaDao rptpatentDataAreaDao;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;
    @Autowired
    private PacPatentinfoDao pacPatentinfoDao;

    /**
     * 专利申请查询按参查询
     */
    @Override
    public Res getWebPatentApplyList(WebPatentApplySearchParam param, Integer platformid) {
        if (param.getPatType()!= null && param.getPatType().size()>0){
            if (param.getPatType().contains(1)) param.getPatType().add(8);
            if (param.getPatType().contains(2)) param.getPatType().add(9);
        }
        //分页
        Page<Object> page = PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<WebPatentApplyModel> models = pacPatentinfoDao.getWebPatentApplyList(param, platformid);
        //转换处理
        List<WebPatentApplyInfo> infos = new ArrayList<>();
        for(WebPatentApplyModel model : models){
            WebPatentApplyInfo info = new WebPatentApplyInfo();
            if(model.getPatTypeName() != null) //1
                info.setPatType(cfgDictionaryService.getNameByCodeAndValue("1003",model.getPatTypeName().toString()));
            info.setApplyPersonType(model.getApplyPersonType());
            info.setPatId(model.getId());
            info.setPatNum(model.getPatNum());
            info.setPatName(model.getPatName());
            info.setApplyPerson(model.getApplyPerson());
            info.setApplyDate(DateUtils.format(model.getPatAppDate()));
            info.setAddress(model.getPatAddress());
            info.setAreaName(model.getAreaName());
            info.setAgencyName(model.getAgencyName());
            infos.add(info);
        }
        Long total = page.getTotal();
        PageResult<WebPatentApplyInfo> result = new PageResult<WebPatentApplyInfo>(infos,param.getPageNumber(),param.getPageSize(),total.intValue());

        return Res.ok().put("list",result);
    }

    /**
     * 专利归类
     */
    @Override
    public Res classifyPat(List<String> patId, Integer areaId) {
//        RptPatentDataAreaEntity areaEntity = rptpatentDataAreaDao.selectByPrimaryKey(areaId);
//        if (areaEntity == null) return  Res.error("未查询到区域数据");
        for(String i :patId){
            PacPatentinfoEntity pacPatentinfoEntity = pacPatentinfoDao.selectByPrimaryKey(Integer.parseInt(i));
            if(pacPatentinfoEntity == null) return Res.error("未查询到专利数据");
            pacPatentinfoEntity.setAreaid(areaId);
            pacPatentinfoDao.updateByPrimaryKey(pacPatentinfoEntity);
        }
        return Res.ok();
    }
}
